#!/usr/bin/env python
# -*- coding:GBK -*-
import os
import os.path
from sgmllib import SGMLParser
class AnalyseJavaHtml(SGMLParser):
    def __init__(self):
      SGMLParser.__init__(self)
      self.ispre=False
      self.isbr=False
      self.sourceCode=''
    def start_pre(self,attrs):
        self.ispre=True
    def handle_data(self, text):
        if self.ispre==True:
            self.sourceCode=text.replace('&lt;','<')
            self.sourceCode=False
if __name__ == '__main__':
    zipFileNameList=[]
    backupPath='E:\\Temp\\plugin-fastpath'
    codeMap={}
    for root,dirnames,filenames in os.walk(backupPath):
        for filename in filenames:
            if filename.find('$')==-1:
                coderoot=root.replace('plugin-fastpath','plugin-fastpath\\src')
                htmlname=filename.replace('.class','.java.html')
                codeMap[htmlname]={}
                codeMap[htmlname]['coderoot']=coderoot
                codeMap[htmlname]['filename']=filename.replace('.class','.java')
                codeMap[htmlname]['filepath']=os.path.join(coderoot,codeMap[htmlname]['filename'])
                print htmlname
                zipFileNameList.append(os.path.join(root,filename))
    #for fileName in zipFileNameList:
        #urlAddress=fileName.replace(backupPath,'http://www.massapi.com/source/openfire_src/src/plugins/fastpath/src/java')
        #urlAddress=urlAddress.replace('\\','/')
        #urlAddress=urlAddress.replace('.class','.java.html')
    for root,dirnames,filenames in os.walk('E:\\download\\fastpath'):
        for filename in filenames:
            preFilename=os.path.join(root,filename)
            preFile=open(preFilename,'r')
            htmlSource=preFile.read()
            preKey='<pre class="brush: java; toolbar: false">'
            preBegin=htmlSource.find(preKey)
            preEnd=htmlSource.find('</pre>',preBegin)
            sourceCode=htmlSource[preBegin+len(preKey):preEnd].replace('&lt;','<')
            savePathPre='E:\\Temp\\fastpath\\src'
            if codeMap.has_key(filename):
                sourceCodeFile=open(codeMap[filename]['filepath'],'w')
                sourceCodeFile.write(sourceCode)
                sourceCodeFile.close()
    #preFilename='E:\\download\\fastpath\\CreateWorkgroup.java.html'
    #print sourceCode
    
